home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / UNIX / LIB / DNS / RES_INIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-21  |  5.6 KB  |  181 lines

  1. #include <fiddle.h>
  2. #define MAXDFLSRCH 10
  3. /*-
  4.  * Copyright (c) 1985, 1989 Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that: (1) source distributions retain this entire copyright notice and
  9.  * comment, and (2) distributions including binaries display the following
  10.  * acknowledgement:  ``This product includes software developed by the
  11.  * University of California, Berkeley and its contributors'' in the
  12.  * documentation or other materials provided with the distribution and in
  13.  * all advertising materials mentioning features or use of this software.
  14.  * Neither the name of the University nor the names of its contributors may
  15.  * be used to endorse or promote products derived from this software without
  16.  * specific prior written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #if defined(LIBC_SCCS) && !defined(lint)
  23. static char sccsid[] = "@(#)res_init.c    6.14 (Berkeley) 6/27/90";
  24. #endif /* LIBC_SCCS and not lint */
  25.  
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <stdio.h>
  30. #include <arpa/nameser.h>
  31. #include <resolv.h>
  32.  
  33. /*
  34.  * Resolver state default settings
  35.  */
  36.  
  37. struct state _res = {
  38.     RES_TIMEOUT,                   /* retransmition time interval */
  39.     4,                             /* number of times to retransmit */
  40.     RES_DEFAULT,            /* options flags */
  41.     1,                             /* number of name servers */
  42. };
  43.  
  44. /*
  45.  * Set up default settings.  If the configuration file exist, the values
  46.  * there will have precedence.  Otherwise, the server address is set to
  47.  * INADDR_ANY and the default domain name comes from the gethostname().
  48.  *
  49.  * The configuration file should only be used if you want to redefine your
  50.  * domain or run without a server on your machine.
  51.  *
  52.  * Return 0 if completes successfully, -1 on error
  53.  */
  54. res_init()
  55. {
  56.     register FILE *fp;
  57.     register char *cp, **pp;
  58.     register int n;
  59.     char buf[BUFSIZ];
  60.     extern u_long inet_addr();
  61.     extern char *index();
  62.     extern char *strcpy(), *strncpy();
  63.     extern char *getenv();
  64.     int nserv = 0;    /* number of nameserver records read from file */
  65.     int haveenv = 0;
  66.     int havesearch = 0;
  67.  
  68.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  69.     _res.nsaddr.sin_family = AF_INET;
  70.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  71.     _res.nscount = 1;
  72.  
  73.     /* Allow user to override the local domain definition */
  74.     if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  75.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  76.         haveenv++;
  77.     }
  78.  
  79.     if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
  80.         /* read the config file */
  81.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  82.         /* read default domain name */
  83.         if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  84.             if (haveenv)    /* skip if have from environ */
  85.                 continue;
  86.             cp = buf + sizeof("domain") - 1;
  87.             while (*cp == ' ' || *cp == '\t')
  88.                 cp++;
  89.             if ((*cp == '\0') || (*cp == '\n'))
  90.                 continue;
  91.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  92.             if ((cp = index(_res.defdname, '\n')) != NULL)
  93.                 *cp = '\0';
  94.             havesearch = 0;
  95.             continue;
  96.         }
  97.         /* set search list */
  98.         if (!strncmp(buf, "search", sizeof("search") - 1)) {
  99.             if (haveenv)    /* skip if have from environ */
  100.                 continue;
  101.             cp = buf + sizeof("search") - 1;
  102.             while (*cp == ' ' || *cp == '\t')
  103.                 cp++;
  104.             if ((*cp == '\0') || (*cp == '\n'))
  105.                 continue;
  106.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  107.             if ((cp = index(_res.defdname, '\n')) != NULL)
  108.                 *cp = '\0';
  109.             /*
  110.              * Set search list to be blank-separated strings
  111.              * on rest of line.
  112.              */
  113.             cp = _res.defdname;
  114.             pp = _res.dnsrch;
  115.             *pp++ = cp;
  116.             for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
  117.                 if (*cp == ' ' || *cp == '\t') {
  118.                     *cp = 0;
  119.                     n = 1;
  120.                 } else if (n) {
  121.                     *pp++ = cp;
  122.                     n = 0;
  123.                 }
  124.             }
  125.             /* null terminate last domain if there are excess */
  126.             while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  127.                 cp++;
  128.             *cp = '\0';
  129.             *pp++ = 0;
  130.             havesearch = 1;
  131.             continue;
  132.         }
  133.         /* read nameservers to query */
  134.         if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
  135.            nserv < MAXNS) {
  136.             cp = buf + sizeof("nameserver") - 1;
  137.             while (*cp == ' ' || *cp == '\t')
  138.                 cp++;
  139.             if ((*cp == '\0') || (*cp == '\n'))
  140.                 continue;
  141.             if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
  142.             inet_addr(cp)) == (unsigned)-1) {
  143.                 _res.nsaddr_list[nserv].sin_addr.s_addr
  144.                 = INADDR_ANY;
  145.                 continue;
  146.             }
  147.             _res.nsaddr_list[nserv].sin_family = AF_INET;
  148.             _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
  149.             nserv++;
  150.             continue;
  151.         }
  152.         }
  153.         if (nserv > 1)
  154.         _res.nscount = nserv;
  155.         (void) fclose(fp);
  156.     }
  157.     if (_res.defdname[0] == 0) {
  158.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  159.            (cp = index(buf, '.')))
  160.             (void)strcpy(_res.defdname, cp + 1);
  161.     }
  162.  
  163.     /* find components of local domain that might be searched */
  164.     if (havesearch == 0) {
  165.         pp = _res.dnsrch;
  166.         *pp++ = _res.defdname;
  167.         for (cp = _res.defdname, n = 0; *cp; cp++)
  168.             if (*cp == '.')
  169.                 n++;
  170.         cp = _res.defdname;
  171.         for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
  172.             n--) {
  173.             cp = index(cp, '.');
  174.             *pp++ = ++cp;
  175.         }
  176.         *pp++ = 0;
  177.     }
  178.     _res.options |= RES_INIT;
  179.     return (0);
  180. }
  181.